
var prefix = "/server/viewChklogCount";
var prefixT = "/server/chklogPointDiAna";
var timestampbegin =631123200000/1000;
var timestampend = 3471264000000/1000;
var zhanhao  = $('.chosen-select-zhanhao').val();
var weizhi = $('.chosen-select-weizhi').val();
var alarmclass = $('.chosen-select-almclass').val();
var alarmlevel = $('.chosen-select-almlevel').val();

$(function() {
	load();
});
function selectLoad() {
    var htmlZh = "";
    var htmlWz = "";
    var htmlCls = "";
    var htmlLevl = "";
    $.ajax({
        url :  prefixT + "/zhanHao",
        success : function(dataZh) {
            //加载数据
            for (var i = 0; i < dataZh.length; i++) {
                if(dataZh[i]!=null){
                    console.log("dataZh i:%d,lenth:%d,data:%s",i,dataZh.length,dataZh[i].zhanhao);
                    htmlZh += '<option value="' + dataZh[i].zhanhao + '">' + dataZh[i].zhanhao + '</option>'
                }
            }
            $(".chosen-select-zhanhao").append(htmlZh);
            $(".chosen-select-zhanhao").chosen({
                maxHeight : 200
            });
            //点击事件
            /*
            $('.chosen-select').on('change', function(e, params) {
                console.log(params.selected);
                var opt = {
                    query : {
                        type : params.selected,
                    }
                }
                $('#exampleTable').bootstrapTable('refresh', opt);
            });
            */
        }
    });
    html = "";
    $.ajax({
        url :  prefixT + "/weiZhi",
        success : function(dataWz) {
            //加载数据
            for (var i = 0; i < dataWz.length; i++) {
                if(dataWz[i]!=null){
                    console.log("dataWz i:%d,lenth:%d,data:%s",i,dataWz.length,dataWz[i].weizhi);
                    htmlWz += '<option value="' + dataWz[i].weizhi + '">' + dataWz[i].weizhi + '</option>'
                }
            }
            $(".chosen-select-weizhi").append(htmlWz);
            $(".chosen-select-weizhi").chosen({
                maxHeight : 200
            });
        }
    });
    html = "";
    $.ajax({
        url :  prefixT + "/almClass",
        success : function(dataCls) {
            //加载数据
            for (var i = 0; i < dataCls.length; i++) {
                if(dataCls[i]!=null && dataCls[i].alarmclass!=""){
                    console.log("dataCls i:%d,lenth:%d,data:%s",i,dataCls.length,dataCls[i].alarmclass);
                    htmlCls += '<option value="' + dataCls[i].alarmclass + '">' + dataCls[i].alarmclass + '</option>'
                }
            }
            $(".chosen-select-almclass").append(htmlCls);
            $(".chosen-select-almclass").chosen({
                maxHeight : 200
            });
        }
    });
    html = "";
    $.ajax({
        url :  prefixT + "/almLevel",
        success : function(dataLevl) {
            //加载数据
            for (var i = 0; i < dataLevl.length; i++) {
                if(dataLevl[i]!=null){
                    console.log("dataLevl i:%d,lenth:%d,data:%s",i,dataLevl.length,dataLevl[i].alarmlevel);
                    htmlLevl += '<option value="' + dataLevl[i].alarmlevel + '">' + dataLevl[i].alarmlevel + '</option>'
                }
            }
            $(".chosen-select-almlevel").append(htmlLevl);
            $(".chosen-select-almlevel").chosen({
                maxHeight : 200
            });
        }
    });
}
function load() {
    selectLoad();
	$('#exampleTable')
			.bootstrapTable(
					{
						method : 'get', // 服务器数据的请求方式 get or post
						url : prefix + "/list", // 服务器数据的加载地址
					//	showRefresh : true,
					//	showToggle : true,
					//	showColumns : true,
						iconSize : 'outline',
						toolbar : '#exampleToolbar',
						striped : true, // 设置为true会有隔行变色效果
						dataType : "json", // 服务器返回的数据类型
						pagination : true, // 设置为true会在底部显示分页条
						// queryParamsType : "limit",
						// //设置为limit则会发送符合RESTFull格式的参数
						singleSelect : false, // 设置为true将禁止多选
						// contentType : "application/x-www-form-urlencoded",
						// //发送到服务器的数据编码类型
						pageSize : 10, // 如果设置了分页，每页数据条数
						pageNumber : 1, // 如果设置了分布，首页页码
						//search : true, // 是否显示搜索框
						showColumns : true, // 是否显示内容下拉框（选择显示的列）
						sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
						queryParams : function(params) {
							return {
								//说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
								limit: params.limit,
								offset:params.offset,
                                zhanhao : $('.chosen-select-zhanhao').val(),
                                checktimebegin :timestampbegin,
                                checktimeend :timestampend,
                                zhanhao  : $('.chosen-select-zhanhao').val(),
                                weizhi : $('.chosen-select-weizhi').val(),
                                alarmclass : $('.chosen-select-almclass').val(),
                                alarmlevel : $('.chosen-select-almlevel').val(),
                                dispdesc:$('#searchDesc').val()
					           // name:$('#searchName').val(),
					           // username:$('#searchName').val()
							};
						},
						// //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
						// queryParamsType = 'limit' ,返回参数必须包含
						// limit, offset, search, sort, order 否则, 需要包含:
						// pageSize, pageNumber, searchText, sortName,
						// sortOrder.
						// 返回false将会终止请求
						columns : [
                            {
                                field : 'countNum',
                                title : '统计个数'
                            },
//                            {
//                                field : 'tag',
//                                title : 'opc的key'
//                            },
                            {
                                field : 'dispdesc',
                                title : '报警信息'
                            },

                                                            {
                                field : 'checkvalue',
                                title : '报警值'
                            },
                                                            {
                                field : 'searchdesc',
                                title : '实时值'
                            },
                                                            {
                                field : 'checktimestamp',
                                title : '报警时间'
                            },
                            {
                                field : 'alarmlevel',
                                title : '告警等级'
                            },
                            {
                                field : 'alarmclass',
                                title : '告警类型'
                            },
                            {
                                field : 'weizhi',
                                title : '位置'
                            },
                                                            {
                                field : 'zhanhao',
                                title : '站号'
                            }
						]
					});
}
function reLoad() {
    zhanhao  = $('.chosen-select-zhanhao').val();
    weizhi = $('.chosen-select-weizhi').val();
    alarmclass = $('.chosen-select-almclass').val();
    alarmlevel = $('.chosen-select-almlevel').val();
    var timebegin = $('#beginTime').val();
    timestampbegin =631123200000/1000;
    if(timebegin !=null && timebegin !="") {
        timebegin = timebegin.substring(0,19);
        timestampbegin = (new Date(timebegin).getTime())/1000;
    }else
        timebegin = "1990-01-01 00:00:00";
    console.log(timestampbegin);
    var timeend = $('#endTime').val();
    timestampend = 3471264000000/1000;
    if(timeend !=null && timeend !="")
    {
        timeend = timeend.substring(0,19);
        timestampend =(new Date(timeend).getTime())/1000;
    }else
        timeend = "2048-01-01 00:00:00";
    console.log(timestampend);
    var schType = $('#searchName').val();
    var opt = {
        query : {
            zhanhao : zhanhao,
            weizhi : weizhi,
            alarmclass : alarmclass,
            alarmlevel : alarmlevel,
            checktimebegin :timestampbegin,
            checktimeend :timestampend,
            chktimebeginstr:timebegin,
            chktimeendstr :timeend,
            dispdesc:$('#searchDesc').val()
        }
    }
    $('#exampleTable').bootstrapTable('refresh', opt);
}
Date.prototype.format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
var g_dlfile2 =0;
function downloadTable() {
    zhanhao  = $('.chosen-select-zhanhao').val();
    weizhi = $('.chosen-select-weizhi').val();
    alarmclass = $('.chosen-select-almclass').val();
    alarmlevel = $('.chosen-select-almlevel').val();
    var timebegin = $('#beginTime').val();
    timestampbegin =631123200000/1000;
    if(timebegin !=null && timebegin !="") {
        timebegin = timebegin.substring(0,19);
        timestampbegin = (new Date(timebegin).getTime())/1000;
    }else
        timebegin = "1990-01-01 00:00:00";
    console.log(timestampbegin);
    var timeend = $('#endTime').val();
    timestampend = 3471264000000/1000;
    if(timeend !=null && timeend !="")
    {
        timeend = timeend.substring(0,19);
        timestampend =(new Date(timeend).getTime())/1000;
    }else
        timeend = "2048-01-01 00:00:00";
    console.log(timestampend);
    var schType = $('#searchName').val();
    var downName;
    if(schType =="1")
        downName="OPC Key 统计表-"+new Date().format("yyyy-MM-dd hh:mm:ss");
    else if(schType =="2")
        downName="告警类别统计表-"+new Date().format("yyyy-MM-dd hh:mm:ss");
    else if(schType =="3")
        downName="告警等级统计表-"+new Date().format("yyyy-MM-dd hh:mm:ss");
    else
        downName="告警位置统计表-"+new Date().format("yyyy-MM-dd hh:mm:ss");

    var opt = {
        query : {
            zhanhao : zhanhao,
            weizhi : weizhi,
            alarmclass :alarmclass,
            alarmlevel : alarmlevel,
            checktimebegin :timestampbegin,
            checktimeend :timestampend,
            chktimebeginstr:timebegin,
            chktimeendstr :timeend,
            dispdesc:$('#searchDesc').val()
        }
    }
    var optMax = {
        query : {
            limit: 10000000,
            offset:0,
            zhanhao : zhanhao,
            weizhi : weizhi,
            alarmclass : alarmclass,
            alarmlevel : alarmlevel,
            checktimebegin :timestampbegin,
            checktimeend :timestampend,
            chktimebeginstr:timebegin,
            chktimeendstr :timeend,
            dispdesc:$('#searchDesc').val()
        }
    }
    g_dlfile2 = 1;
    $('#exampleTable').bootstrapTable('refresh', optMax);
    $('#exampleTable').on('load-success.bs.table',function(){
        console.log("load success,gfl:%d",g_dlfile2);
        if(g_dlfile2 == 1)
        {
            $('#exampleTable').tableExport({ type: 'excel', escape: 'false',fileName:downName,exportDataType: 'all' });
            g_dlfile2 = 0;
            $('#exampleTable').bootstrapTable('refresh', opt);
        }
    });
}
function add() {
	layer.open({
		type : 2,
		title : '增加',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/add' // iframe的url
	});
}
function edit(id) {
	layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/edit/' + id // iframe的url
	});
}
function remove(id) {
	layer.confirm('确定要删除选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/remove",
			type : "post",
			data : {
				'checkatid' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}

function resetPwd(id) {
}
function batchRemove() {
	var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要删除的数据");
		return;
	}
	layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		var ids = new Array();
		// 遍历所有选择的行数据，取每条数据对应的ID
		$.each(rows, function(i, row) {
			ids[i] = row['checkatid'];
		});
		$.ajax({
			type : 'POST',
			data : {
				"ids" : ids
			},
			url : prefix + '/batchRemove',
			success : function(r) {
				if (r.code == 0) {
					layer.msg(r.msg);
					reLoad();
				} else {
					layer.msg(r.msg);
				}
			}
		});
	}, function() {

	});
}